<?php
class Product_IndexController extends Lin_Template
{
	private $item;
	private $userListSql = "SELECT `item_id`, `item_sku`, `item_description`, `item_cost`, `item_sales_price`, `item_quantity`, `item_length`, `item_width`, `item_height`, 
			round(`item_length` * `item_width` * `item_height` / 5000,2) as `VWT`,  `item_weight`
				FROM item";
	
	public function __construct()
	{
		$this->item = new Model_Product_Item();
	}
	
	public function itemList()
	{
		$grid = new Lin_Grid('itemlist');
		$grid->setGrid(array('url' => '/product/index/_itemList', 'caption' => 'Item List'));
		$grid->setQuery($this->userListSql);
		$grid->setColName(array('id', 'sku', 'description', 'cost', 'sales price', 'quantity', 'length', 'width', 'height', 'VWT', 'weight'));
		$grid->setColModel(0, array('hidden' => true, 'width' => 25));
		$grid->setColModel(1, array('width' => 100, ));
		$grid->setColModel(2, array('width' => 300));
		$grid->setColModel(3, array('width' => 30, 'align' => 'right'));
		$grid->setColModel(4, array('width' => 30, 'align' => 'right'));
		$grid->setColModel(5, array('hidden' => false,'width' => 30, 'align' => 'right'));
		$grid->setColModel(6, array('hidden' => true,'width' => 30, 'align' => 'right'));
		$grid->setColModel(7, array('hidden' => true,'width' => 30, 'align' => 'right'));
		$grid->setColModel(8, array('hidden' => true,'width' => 30, 'align' => 'right'));
		$grid->setColModel(9, array('width' => 30, 'align' => 'right'));
		$grid->setColModel(10, array('width' => 30, 'align' => 'right'));
		$grid->setSelectRow('item_id', '#edit-button', "editItem");
		$grid->setSelectRow('item_id', '#del-button', "delItem");
		return $grid->html();		
	}
	
	public function _itemListAction()
	{
		$grid = new Lin_Grid('itemlist');		
		$grid->setKey('item_id');
		$grid->setQuery($this->userListSql);	
		echo $grid->json();	
	}
	
	
	public function indexAction()
	{
		
	}
	
	public function addAction()
	{
		if (isset($_POST['action']) && $_POST['action'] == 'add')
		{
			$itemSku = trim($_POST['item_sku']);
			$dt  = array('item_sku' => $itemSku,
					'item_description' => trim($_POST['item_description']),
					'item_cost' => trim($_POST['item_cost']),
					'item_quantity' => trim($_POST['item_quantity']),
					'item_length' => trim($_POST['item_length']),
					'item_width' => trim($_POST['item_width']),
					'item_height' => trim($_POST['item_height']),
					'item_weight' => trim($_POST['item_weight'])
					);
			if ($this->item->itemIsExistBySku($itemSku))
			{
				Lin_Dialog::msg('Product Add', 'Add product unsuccessfully.<br/> SKU already exist.', -1);
				exit;
			}
			if ($this->item->put($dt))
			{
				Lin_Dialog::msg('Product Add', 'Add product item success', '/product/index/add/');
			}
			exit;
		}
	}
	
	public function getItem()
	{
		$rs = Lin_Request::getInstance();
		$args = $rs->getArgs();
		$id = $args[3];
		return $this->item->get($id);
	}
	
	public function editAction()
	{
		if (isset($_POST['action']) && $_POST['action'] == 'edit')
		{
			$itemId = $_POST['item_id'];
			$itemSku = trim($_POST['item_sku']);
			$dt  = array('item_sku' => $itemSku,
					'item_description' => trim($_POST['item_description']),
					'item_cost' => trim($_POST['item_cost']),
					'item_quantity' => trim($_POST['item_quantity']),
					'item_length' => trim($_POST['item_length']),
					'item_width' => trim($_POST['item_width']),
					'item_height' => trim($_POST['item_height']),
					'item_weight' => trim($_POST['item_weight'])
			);
			$item = $this->getItem();
			if ($item['item_sku'] != $itemSku)
			{
				if ($this->item->itemIsExistBySku($itemSku))
				{
					Lin_Dialog::msg('Product Edit', 'Edit product unsuccessfully.<br/> SKU already exist.', -1);
					exit;
				}
			}
			if ($this->item->set($itemId, $dt))
			{
				Lin_Dialog::msg('Product Edit', 'Edit product item success', '/product/index/edit/' . $itemId);
			}
			exit;			
		}
	}
	
	public function _delAction()
	{
		$rs = Lin_Request::getInstance();
		$args = $rs->getArgs();
		$id = $args[3];
		if ($this->item->remove($id))
		{	
			Lin_Dialog::msg('Product Del', 'Del product item success', 1, '/product/');
		}
	}
	
	private function countSalesPrice($cost)
	{
		$profitBase 	= $cost * 0.33;	//利润因子
		$minProfit		= 0;
		$ad 			= 0;			//广告费
		$shippingFee	= 170;
		$shippingCharge	= 9.99;
		$otherFee		= 20;
		$rate			= 6.30;
		$paypalHandlingRate	= 0.04;		
		return round(($cost + $profitBase + $minProfit + $ad + $shippingFee + $otherFee) * (1 + $paypalHandlingRate)/$rate, 2) - round($shippingCharge,2);
	}
	
	public function _countSalesPriceAction()
	{
		$item = $this->item->getAll(1, array('item_id', 'item_cost'));
		foreach ($item as $val)
		{
			$this->item->set($val['item_id'], array('item_sales_price'=> $this->countSalesPrice($val['item_cost'])));
		}
		
		Lin_Dialog::msg('Count sales price', 'Have done !', '/product/');
	}
}